import { _decorator, Node, view } from "cc";
import { GameNativeAd } from "./NativeAd/GameNativeAd";
import { FinishNativeAd } from "./NativeAd/FinishNativeAd";
import EventManager from "../../../Common/EventManager";
import { EventType } from "../../../GameSpecial/GameEventType";

const { ccclass } = _decorator;

@ccclass
export default class HeyGamePlatform {
    private static _instance: HeyGamePlatform;
    private gameNativeAd: GameNativeAd = null;
    private finishNativeAd: FinishNativeAd = null;

    constructor() {
        console.log("HeyGamePlatform constructor");
        HeyGameConfig.HeyGameSDKConfig.instance.init();
    }

    public init(openId: string = null) {
        if (!!openId) {
            localStorage.setItem('hey_game_openId', openId);
        }
        HeyGameAdSDK.GameDataSDK.instance.initGameSDK();
        HeyGameAdSDK.GameAdSDK.instance.initGameSDK(() => {
            if (HeyGameConfig.HeyGameSDKConfig.onVIVOMiniGame) {
                //vivo暂停音效
                // audioEngine.stopAllEffects();
            }
        });
        this.initGameSDK();
    }

    public static get instance(): HeyGamePlatform {
        if (!HeyGamePlatform._instance) {
            HeyGamePlatform._instance = new HeyGamePlatform();
        }
        return HeyGamePlatform._instance;
    }

    /**
     * 初始化SDK
     */
    private initGameSDK() {
        if (HeyGameConfig.HeyGameSDKConfig.onOPPOMiniGame || HeyGameConfig.HeyGameSDKConfig.onVIVOMiniGame
            || HeyGameConfig.HeyGameSDKConfig.onMobile
            || HeyGameConfig.HeyGameSDKConfig.onPC
        ) {
            this.gameNativeAd = new GameNativeAd();
            this.finishNativeAd = new FinishNativeAd();
        }
    }

    /**
     * 创建原生广告节点
     */
    public initAdSDK(node: Node, style: any = null) {
        if (HeyGameConfig.HeyGameSDKConfig.onOPPOMiniGame || HeyGameConfig.HeyGameSDKConfig.onVIVOMiniGame
            || HeyGameConfig.HeyGameSDKConfig.onMobile
            || HeyGameConfig.HeyGameSDKConfig.onPC
        ) {
            this.gameNativeAd.initNativeStyle(style);
            this.gameNativeAd.addNodeToParent(node);
            this.finishNativeAd.initNativeStyle(style);
            this.finishNativeAd.addNodeToParent(node);
        }
    }

    /**
     * 游戏中原生广告 OPPO,VIVO
     * @param style     原生广告类型，原生大图和原生banner
     * @param bgBtn     原生广告大背景，提高原生点击率
     * @param viewBtn   查看广告按钮
     * @param widget    广告节点适配参数
     */
    public showGameNativeAd(style: any = null, bgBtn: any = null, viewBtn: any = null, widget?: any) {
        if (HeyGameConfig.HeyGameSDKConfig.onVIVOMiniGame || HeyGameConfig.HeyGameSDKConfig.onTTMiniGame) {
            if (HeyGameAdSDK.GameAdSDK.SDK.isOpen) {
                HeyGameAdSDK.GameAdSDK.SDK.showInsertAd(201);
            }
        }
        if (this.gameNativeAd) {
            this.gameNativeAd.isStopAutoShowNativeAd = false;
            if (this.gameNativeAd.bgBtn) {
                this.gameNativeAd.bgBtn.off('click');
                this.gameNativeAd.bgBtn = null;
            }
            if (this.gameNativeAd.viewAdBtn) {
                this.gameNativeAd.viewAdBtn.off('click');
                this.gameNativeAd.viewAdBtn = null;
            }
            this.gameNativeAd.bgBtn = bgBtn;
            if (this.gameNativeAd.bgBtn) {
                this.gameNativeAd.bgBtn.on('click', (button) => {
                    this.gameNativeAd.onNativeAdClick();
                }, this);
            }
            this.gameNativeAd.viewAdBtn = viewBtn;
            if (this.gameNativeAd.viewAdBtn) {
                this.gameNativeAd.viewAdBtn.on('click', (button) => {
                    this.gameNativeAd.onNativeAdClick();
                }, this);
            }

            this.gameNativeAd.showNativeAd(style, widget);
        } else {
            HeyGameAdSDK.GameAdSDK.SDK.showBannerAd(105);
        }
    }

    public hideGameNativeAd() {
        if (this.gameNativeAd)
            this.gameNativeAd.hideNativeAd(true);
    }

    /**
     * 结算中原生广告 OPPO,VIVO
     * @param style 原生广告类型，原生大图和原生banner
     * @param bgBtn 原生广告大背景，提高原生点击率
     * @param viewBtn 查看广告按钮
     * @param widget    广告节点适配参数
     */
    public showFinishNativeAd(style: any = null, bgBtn: any = null, viewBtn: any = null, widget?: any) {
        this.hideGameNativeAd();
        // HeyGameAdSDK.GameAdSDK.SDK.endGameEvent(this);
        if (this.finishNativeAd) {
            if (this.finishNativeAd.bgBtn) {
                this.finishNativeAd.bgBtn.off('click');
                this.finishNativeAd.bgBtn = null;
            }
            if (this.finishNativeAd.viewAdBtn) {
                this.finishNativeAd.viewAdBtn.off('click');
                this.finishNativeAd.viewAdBtn = null;
            }
            this.finishNativeAd.bgBtn = bgBtn;
            if (this.finishNativeAd.bgBtn) {
                this.finishNativeAd.bgBtn.active = false;
                this.finishNativeAd.bgBtn.on('click', (button) => {
                    this.finishNativeAd.onNativeAdClick();
                }, this);
            }
            this.finishNativeAd.viewAdBtn = viewBtn;
            if (this.finishNativeAd.viewAdBtn) {
                this.finishNativeAd.viewAdBtn.active = false;
                this.finishNativeAd.viewAdBtn.on('click', (button) => {
                    this.finishNativeAd.onNativeAdClick();
                }, this);
            }
            this.finishNativeAd.showNativeAd(style, widget);
        }
    }

    public hideFinishNativeAd() {
        if (this.finishNativeAd)
            this.finishNativeAd.hideNativeAd();
    }

    /**
     *  添加桌面按钮
     * @param success true 发放添加桌面奖励
     */
    public addIconToDesktop(success: Function) {
        console.log('addIconToDesktop');
        HeyGameAdSDK.GameAdSDK.SDK.hasShortcutInstalled((res) => {
            if (res) {
                success(false);
            } else {
                HeyGameAdSDK.GameAdSDK.SDK.installShortcutIcon((res) => {
                    if (res) {
                        HeyGameAdSDK.GameAdSDK.SDK.hasShortcutInstalled((res) => {
                            success(res);
                        });
                    } else {
                        success(false);
                    }
                });
            }
        });
    }

    /**
     * 展示oppo主界面九宫格更多游戏
     */
    public showMoreGame() {
        HeyGameAdSDK.GameAdSDK.SDK.showPlatformDrawerGameAd((status) => {
            if (status) {
                HeyGameAdSDK.GameAdSDK.SDK.hideBannerAd();
                this.gameNativeAd && this.gameNativeAd.hideNativeAd(true);
            } else {
                if (this.gameNativeAd) {
                    this.gameNativeAd.isStopAutoShowNativeAd = false;
                    this.gameNativeAd.showNativeAd();
                }
            }
        });
    }

    /**
     * oppo结算原生banner互推接口
     */
    public showFinishGameBannerAd() {
        HeyGameAdSDK.GameAdSDK.SDK.showPlatformBannerGameAd();
    }

    /**
     * oppo结算原生banner互推接口
     */
    public hideFinishGameBannerAd() {
        HeyGameAdSDK.GameAdSDK.SDK.hidePlatformBannerGameAd();
    }

    /**
     * 开始游戏数据统计
     */
    public startGameEvent() {
        console.log('startGameEvent')
        HeyGameAdSDK.GameAdSDK.SDK.startGameEvent(this);
    }

    /**游戏结束 */
    public endGameEvent() {
        console.log('endGameEvent')
        HeyGameAdSDK.GameAdSDK.SDK.endGameEvent(this);
    }
    /**
     * 关卡统计 关卡开始
     * @param levelId 关卡id
     */
    public onLevelBegin(levelId: number) {
        HeyGameAdSDK.GameAdSDK.SDK.onBegin("" + (20000000 + levelId));
    }

    /**
     * 关卡统计 关卡完成
     * @param levelId 关卡id
     */
    public onLevelCompleted(levelId: number) {
        HeyGameAdSDK.GameAdSDK.SDK.onCompleted("" + (20000000 + levelId));
    }

    /**
     * 关卡统计 关卡失败
     * @param levelId 关卡id
     * @param isStop 是否主动退出关卡
     */
    public onLevelFailed(levelId: number, isStop: boolean) {
        let failData = {
            missionId: '' + (20000000 + levelId),
            isStop: isStop
        }
        HeyGameAdSDK.GameAdSDK.SDK.onFailed(failData);
    }

    /**
     *
     * @param location 广告调用位置编号 200开始
     */
    public showInsertAd(location) {
        HeyGameAdSDK.GameAdSDK.SDK.showInsertAd(location);
    }

    /**
     *
     * @param location 调用位置标识 300,301,302...
     * @param callback true 发放道具
     */
    public showVideoAd(location: number = 300, callback) {
        HeyGameAdSDK.GameAdSDK.SDK.showVideoAd(location, (result) => {
            switch (result) {
                case 0:
                    callback && callback(true);
                    break;
                case 1:
                    callback && callback(false);
                    break;
                case 2:
                default:
                    callback && callback(false);
                    break;
            }

        }, (result) => {
            if (result) {
                if (HeyGameConfig.HeyGameSDKConfig.onVIVOMiniGame) {
                    //cc.audioEngine.stopAllEffects();
                    //关闭BGM
                    EventManager.emit(EventType.Common.AudioEvent.pause);
                }
            } else {
                if (HeyGameConfig.HeyGameSDKConfig.onVIVOMiniGame) {
                    //todo 恢复背景音乐播放
                    //恢复BGM
                    EventManager.emit(EventType.Common.AudioEvent.resume);
                }
            }
        });
    }
}

